package com.ruoyi.project.hcy.administrations.util;

import com.ruoyi.project.hcy.administrations.domain.*;
import com.ruoyi.project.hcy.administrations.service.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @ClassName ApproveAgreeUtil
 * @Author 金刚芭比
 * @Date 2020/9/20 15:17
 * @Version 1.0
 */
@Component
public class ApproveAgreeUtil {
    @Autowired
    private IApproveService approveService;
    @Autowired
    private ICrewService crewService;
    @Autowired
    private IDimissionService dimissionService;//离职
    @Autowired
    private IInviteService inviteService; //入职
    @Autowired
    private IPostingService postingService;  //职位调整
    @Autowired
    private ITrainService trainService;  // 培训
    public int Agree(Integer id){
        Dimission dimission = null;
        Invite invite = null;
        Posting posting = null;
        Train train = null;
        Crew crew;
        int pass = 0;
        //根据拿到的id查询数据
        Approve approve = approveService.selectApproveById(id);
        if ("离职申请".equals(approve.getApproveType())){
            dimission = new Dimission();
            dimission.setDimissionName(approve.getApproveName());
            dimission.setDimissionStatus("已通过审批");
            //修改状态
            dimissionService.updateDimissionStatus(dimission);
            //查询离职申请表得到信息
            dimissionService.selectDimissionByName(approve.getApproveName());
            //在人员表设置人员的状态为离职
            crew = new Crew();
            crew.setCrewName(approve.getApproveName());
            crew.setCrewStatus("离职");
            crewService.updateCrewStatus(crew);
            pass = updateAppStatus(approve);
        }else if ("招聘申请".equals(approve.getApproveType())){
            invite = new Invite();
            invite.setInviteName(approve.getApproveName());
            invite.setInviteStatus("已通过审批");
            inviteService.updateInviteStatus(invite);
            Invite invite1 = inviteService.selectInviteByName(invite.getInviteName());
            crew = new Crew();
            crew.setCrewName(invite1.getInviteName());
            crew.setCrewJob(invite1.getInviteJob());
            crew.setCrewCreatDate(new Date());
            crew.setCrewPhone(invite1.getInvitePhone());
            crew.setCrewRealpay(Double.valueOf(invite1.getInviteExsalary()));
            crew.setCrewSex(invite1.getInviteSex());
            crew.setCrewStatus("在职");
            crew.setCrewDepartment(invite1.getInviteDepartment());
            crewService.insertCrew(crew);
            pass = updateAppStatus(approve);
        }else if ("职位调整申请".equals(approve.getApproveType())){
            posting = new Posting();
            posting.setPostingName(approve.getApproveName());
            posting.setPostingStatus("已通过审批");
            postingService.updatePostingStatus(posting);
            Posting data = postingService.selectPostingByName(posting.getPostingName());
            crew = new Crew();
            crew.setCrewJob(data.getPostingNewjob());
            crew.setCrewName(data.getPostingName());
            crewService.updateCrewJob(crew);
            pass = updateAppStatus(approve);
        }else {
            train = new Train();
            train.setTrainName(approve.getApproveName());
            train.setTrainStatus("已通过审批");
            trainService.updateTrainStatus(train);
            crew = new Crew();
            crew.setCrewName(approve.getApproveName());
            crew.setCrewStatus("外出培训");
            crewService.updateCrewStatus(crew);
            pass = updateAppStatus(approve);
        }
        return pass;
    }
    public int updateAppStatus(Approve approve){
        Approve app = new Approve();
        app.setApproveName(approve.getApproveName());
        app.setApproveStatus("已审批");
        app.setApproveType(approve.getApproveType());
        System.out.println(app);
        return approveService.updateApproveStatus(app);
    }

}
